﻿var vm = new Vue({
    el: '#app'
    , data: {
        user: {
            user_no: ''
            , user_name: ''
            , user_mobile: ''
            , user_email: ''
            , user_sex: ''
        }
        , tableData: {
            records: []
            , total: 0
        }
        , avatar: {
            url: ''
        }
        , options: [{
            label: '男'
            , value: "1"
        }, {
            label: '女'
            , value: "0"
        }]
    }
    , mounted: async function () {
        this.getUserInfo();
        await this.getLoginType();
        await this.getLoginTerminal();
        await this.getStatus();
        this.getLoglogin();
    }
    , computed: {
        loginType: function () {
            let result = {};
            this.loginTypeArr.forEach(item => {
                if (item.sub_value) {
                    result[item.sub_value] = item.sub_name;
                }
            });
            return result;
        }
        , loginTerminal: function () {
            let result = {};
            this.loginTerminalArr.forEach(item => {
                if (item.sub_value) {
                    result[item.sub_value] = item.sub_name;
                }
            });
            return result;
        }
        , status: function () {
            let result = {};
            this.statusArr.forEach(item => {
                if (item.sub_value) {
                    result[item.sub_value] = item.sub_name;
                }
            });
            return result;
        }
    }
    , methods: {
        getUserInfo: async function () {
            let me = this;
            await hcCore.post({
                url: '/Personal/Get'
                , success: function (rsp) {
                    me.user.user_no = rsp.data.user_no;
                    me.user.user_name = rsp.data.user_name;
                    me.user.user_mobile = rsp.data.user_mobile;
                    me.user.user_email = rsp.data.user_email;
                    me.user.user_sex = rsp.data.user_sex;
                    me.avatar.url = '/resources/avatar/' + rsp.data.avatar + '?v=' + Math.random();
                }
            });
        }
        , onUpdate: async function () {
            let me = this;
            await hcCore.post({
                url: '/Personal/Update'
                , param: {
                    user_name: me.user.user_name
                    , user_mobile: me.user.user_mobile
                    , user_email: me.user.user_email
                    , user_sex: me.user.user_sex
                }
                , success: '修改成功'
                , error: '修改失败'
            });
        }
        , getLoglogin: async function () {
            let me = this;
            hcCore.post({
                url: '/Personal/LoginLogs'
                , success: function (rsp) {
                    me.tableData.records = rsp.data;
                    me.tableData.total = rsp.total;
                }
            });
        }
        , getLoginType: async function () {
            let me = this;
            await hcCore.post({
                url: '/SysSetting/GetLoginType'
                , success: async function (rsp) {
                    me.loginTypeArr = rsp.data;
                    me.loginTypeArr.unshift({
                        sub_value: ''
                        , sub_name: '请选择'
                    });
                }
            });
        }
        , getLoginTerminal: async function () {
            let me = this;
            await hcCore.post({
                url: '/SysSetting/GetLoginTerminal'
                , success: async function (rsp) {
                    me.loginTerminalArr = rsp.data;
                    me.loginTerminalArr.unshift({
                        sub_value: ''
                        , sub_name: '请选择'
                    });
                }
            });
        }
        , getStatus: async function () {
            let me = this;
            await hcCore.post({
                url: '/SysSetting/GetStatus'
                , success: async function (rsp) {
                    me.statusArr = rsp.data;
                    me.statusArr.unshift({
                        sub_value: ''
                        , sub_name: '请选择'
                    });
                }
            });
        }
        , getStatusClass: function (status) {
            if (status == 0) {
                return 'el-tag el-tag--danger el-tag--light';
            }
            else {
                return '';
            }
        }
        , handleAvatarSuccess(res, file) {
            if (res && res.success === true) {
                this.avatar.url = URL.createObjectURL(file.raw);
                hcCore.showSuccess(this.avatar.url);
            }
            else {
                hcCore.showError(res.message);
            }
        }
        , handleAvatarError(res, file) {
            console.log(res);
        }
        , beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                //this.$message.error('上传头像图片只能是 JPG 格式!');
                hcCore.showError(file.type);
            }
            if (!isLt2M) {
                hcCore.showError('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
});